const express = require('express');
const connection = require('../mysql') // 获取连接实例
const app = express();
const $sql = require('../db');

app.get('/getallreqdata',(req,res) => {
	var sql = $sql.procure_request.getallreqdata;
	connection.query(sql,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据whether获取采购申请
app.post('/getreqdataby',(req,res) => {
	var sql = $sql.procure_request.getreqdataby;
	var params = [req.body.whether];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据操作人获取采购申请
app.post('/getreqbyname',(req,res) => {
	var sql = $sql.procure_request.getreqbyname;
	var params = [req.body.account];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据id获取采购请求
app.post('/getreqdatabyid',(req,res) => {
	var sql = $sql.procure_request.getreqdatabyid;
	var params = [req.body.id];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据操作人和状态获取采购申请
app.post('/getreqbyidname',(req,res) => {
	var sql = $sql.procure_request.getreqbyidname;
	var params = [req.body.whether,req.body.account];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据id删除采购请求
app.delete('/delrequest',(req,res) => {
	var sql = $sql.procure_request.delrequest;
	var params = req.body.id;
	connection.query(sql,params,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//更新采购请求
app.put('/uprequest', (req, res) => {
	var sql = $sql.procure_request.uprequestdata;
    var params = [req.body.wname,req.body.data,req.body.stoname,req.body.stoid,req.body.num,req.body.stospe,
				 req.body.purchase_price,req.body.amount,req.body.supid,req.body.supname,req.body.bank_account,
				 req.body.whentryname,req.body.title,req.body.id]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//添加采购请求
app.put('/requestadd', (req, res) => {
	var sql = $sql.procure_request.requestadd;
    var params = [req.body.wname,req.body.data,req.body.stoname,req.body.stoid,req.body.stospe,
				  req.body.num,req.body.purchase_price,req.body.amount,req.body.supid,req.body.supname,
				  req.body.bank_account,req.body.whentryname,req.body.whether,req.body.title]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//更新完成状态
app.put('/upwhether', (req, res) => {
	var sql = $sql.procure_request.upwhether;
    var params = [req.body.whether,req.body.id]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})

module.exports = app;